package com.cheng.xbjy.dao;

import com.cheng.xbjy.entity.Meeting;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface MeetingDao extends JpaRepository<Meeting, Long> {

    Page<Meeting> findByTitleLike(String title, Pageable pageable);

    Page<Meeting> findByTitleLikeAndStatus(String title, Long status, Pageable pageable);

    @Query("select count(mj) from MeetingJoin mj where mj.mId = ?1 and mj.uId = ?2")
    Integer findByMidAndUid(Long meetingId, Long userId);

    @Query("select count(mj) from MeetingJoin mj where mj.mId = ?1")
    Integer countByMid(Long meetingId);

    @Query("delete from MeetingJoin mj where mj.mId = ?1 and mj.uId = ?2")
    @Modifying
    void deleteByMidAndUid(Long meetingId, Long userId);

    @Query(value = "insert into meeting_join values(?1,?2)", nativeQuery = true)
    @Modifying
    void insertByMidAndUid(Long meetingId, Long userId);

    List<Meeting> findByStatusNot(Long status);

    @Query("update Meeting m set m.status = ?2 where m.id = ?1")
    @Modifying
    void updateStatus(Long id, Long status);

}
